﻿using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using WorldMapNew;

namespace Tools.Maptools.Editor
{
    [Serializable]
    public class TrafficLight
    {
        [Tooltip("路灯预制体")]
        public GameObject lightObj;
        [Tooltip("路灯控制交通方向 A（左1 上2 右3 下4）")]
        public int controlRoadId1 = 0;
        [Tooltip("路灯控制交通方向 B（左1 上2 右3 下4）")]
        public int controlRoadId2 = 0;
    }

    public class TrafficNode : MonoBehaviour
    {
#if UNITY_EDITOR
        [DrawGizmo(GizmoType.Pickable)]
        void OnDrawGizmos()
        {
            Gizmos.color = Color.black;
            Gizmos.DrawSphere(transform.position,scale);
        }
#endif
        [Tooltip("车道数量")]
        public int roadLineNum = 0;
 
        [Tooltip("路灯信息集合")]
        public List<TrafficLight> trafficLights = new List<TrafficLight>();
        // private
        public Dictionary<int, TrafficPoint> trafficPoints = new Dictionary<int, TrafficPoint>();
        public int nodeId = 0;
        public float scale = 0.4f;
        public void Init(int idx)
        {
            nodeId = idx;
            gameObject.name = "Node_" + idx;
            // 从预制体里面重新绑定各路点
            for (int i = 0; i < transform.childCount; i++)
            {
                TrafficPoint point = transform.GetChild(i).GetComponent<TrafficPoint>();
                if (point) trafficPoints[point.pointId] = point;
            }
        }

        public void CreateTrafficPoint()
        {
            GameObject point = new GameObject();
            point.transform.parent = transform;
            point.transform.localPosition = new Vector3(1,0,1);
            TrafficPoint tp = point.AddComponent<TrafficPoint>();
            //
            //EditorGUIUtility.PingObject(point);
            //Selection.activeGameObject = point;
            //SceneView.lastActiveSceneView.FrameSelected();
        }

        public TrafficPoint GetTrafficPoint(int pointId)
        {
            TrafficPoint res = null;
            if (trafficPoints.ContainsKey(pointId))
            {
                res = trafficPoints[pointId];
            }
            return res;
        }

        public void SetTrafficPoint(int pointId, TrafficPoint point)
        {
            trafficPoints[pointId] = point;
        }

        public int GetTrafficPointId(int x, int y)
        {
            int id = 0;
            if (roadLineNum != 0)
            {
                if (x == 0)
                {
                    id = 10 + y;
                }
                else if (y == roadLineNum + 1)
                {
                    id = 20 + x;
                }
                else if (x == roadLineNum + 1)
                {
                    id = 30 + y;
                }
                else if (y == 0)
                {
                    id = 40 + x;
                }
            }
           
            return id;
        }

        public TrafficNodeData GetNodeData()
        {
            TrafficNodeData data = new TrafficNodeData();
            data.nodeId = nodeId;
            for (int i = 0;i < trafficLights.Count;i++)
            {
                TrafficLight light = trafficLights[i];
                if (light.lightObj == null) continue;
                TileElement element = new TileElement();
                element.position = light.lightObj.transform.position;
                element.rotation = light.lightObj.transform.eulerAngles;
                element.scale = light.lightObj.transform.localScale;
                element.resID = 4; // 先写死路灯resId
                data.addLightData(element, light.controlRoadId1, light.controlRoadId2);
            }
            for (int i = 0; i < transform.childCount; i++)
            {
                TrafficPoint point = transform.GetChild(i).GetComponent<TrafficPoint>();
                if(point) data.addPointData(point.pointId, point.transform.position, point.GetRoadPointData());
            }
            return data;
        }
    }

}
